package com.yanq.liancar.activity;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.ShopOrderListAdapter;
import com.yanq.liancar.adapter.OrderPagerAdapter;
import com.yanq.liancar.bean.OrderListBean;
import com.yanq.liancar.fragment.BOrderFragment;
import com.yanq.liancar.util.Utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SmallBShopOrderListActivity extends BaseToolbarActivity {
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private List<String> titles;
    private String where;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_b_shop_orderlist);
        Utils.setStatusBar(this, true, false);
        where = getIntent().getExtras().getString("where");
        mTvTitle.setText("商城订单");
        mIvRight.setVisibility(View.GONE);

        ButterKnife.bind(this);
        initViewPager();

    }


    private void initViewPager() {
        // 创建一个集合,装填Fragment
        List<Fragment> fragments = new ArrayList<>();
        // 装填
        fragments.add(BOrderFragment.newInstance(0, where));
        fragments.add(BOrderFragment.newInstance(1, where));
        fragments.add(BOrderFragment.newInstance(2, where));
        fragments.add(BOrderFragment.newInstance(3, where));
       fragments.add(BOrderFragment.newInstance(4, where));
       fragments.add(BOrderFragment.newInstance(5, where));

        titles = new ArrayList<>();
        titles.add("全部");
        titles.add("待支付");
        titles.add("待发货");
        titles.add("待收货");
        titles.add("待评价");
        titles.add("已完成");
        //titles.add("已退款");
        // 创建ViewPager适配器
        OrderPagerAdapter myPagerAdapter = new OrderPagerAdapter(getSupportFragmentManager(), fragments, titles);

        // 给ViewPager设置适配器
        viewPager.setAdapter(myPagerAdapter);
        // 使用 TabLayout 和 ViewPager 相关联
        mTabLayout.setupWithViewPager(viewPager);
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabLayout, 15, 15);
            }
        });
    }


    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
}
